import { io } from 'socket.io-client'
import { defineStore } from "pinia"
export const useSocket = defineStore('socket', () => {
    let socket = $ref(null);
    let videoDanmuList = $ref([])
    const initialize = () => {
        socket = io("ws://localhost:8081", {
            reconnectionDelayMax: 10000,
            reconnectionDelay: 1000,
            transports: ['websocket', 'polling'],
        });
        socket.on('connect', () => {
            console.log('connected')
        })
        onBulletChat()
    }
    // 发送弹幕
    const handleAddDanmu = (data:any) => {
        socket.emit('bulletChat', data)
    }

    //监听bulletChat实践
    const onBulletChat = () => {
        socket.on('message', (data => {
            videoDanmuList.push(data)
        }))
    }

    return {
        initialize,
        handleAddDanmu,
        videoDanmuList
    }
})